set(CMAKE_SYSTEM_NAME QNX)
set(CMAKE_SYSTEM_PROCESSOR aarch64le)


if(CMAKE_HOST_LINUX)
    set(HOST_EXECUTABLE_SUFFIX "")
endif(CMAKE_HOST_LINUX)

if(CMAKE_HOST_WINDOWS)
    set(HOST_EXECUTABLE_SUFFIX ".exe")
endif(CMAKE_HOST_WINDOWS)

find_path(QNX_HOST
    NAME usr/bin/qcc${HOST_EXECUTABLE_SUFFIX}
    PATHS $ENV{QNX_HOST}
    NO_CMAKE_PATH
    NO_CMAKE_ENVIRONMENT_PATH
    )

find_path(QNX_TARGET
    NAME usr/include/errno.h
    PATHS $ENV{QNX_TARGET}
    NO_CMAKE_PATH
    NO_CMAKE_ENVIRONMENT_PATH
    )

if(CMAKE_HOST_LINUX)
    find_path(QNX_CONFIGURATION
        NAME /etc/qnx/bin/qnxactivate
        PATHS $ENV{QNX_CONFIGURATION}
        "$ENV{QNX_HOST}/usr/bin/qconfig"
        NO_CMAKE_PATH
        NO_CMAKE_ENVIRONMENT_PATH
        )
endif(CMAKE_HOST_LINUX)

set(ENV{QNX_HOST} ${QNX_HOST})
set(ENV{QNX_TARGET} ${QNX_TARGET})

if(CMAKE_HOST_LINUX)
    set(ENV{QNX_CONFIGURATION} ${QNX_CONFIGURATION})
    set(ENV{PATH} "$ENV{PATH};${QNX_HOST}/usr/bin")
endif(CMAKE_HOST_LINUX)

set(TOOLCHAIN_PREFIX aarch64-unknown-nto-qnx7.0.0-)
set(TOOLCHAIN_TRIPLE aarch64-unknown-nto-qnx7.0.0)

# set compiler triple
set(triple ${TOOLCHAIN_TRIPLE})
set(CMAKE_ASM_COMPILER clang)
set(CMAKE_ASM_COMPILER_TARGET ${triple})
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET ${triple})
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET ${triple})

set(CMAKE_MAKE_PROGRAM "${QNX_HOST}/usr/bin/make"    CACHE PATH "QNX Make Program")
set(CMAKE_SH           "${QNX_HOST}/usr/bin/sh"      CACHE PATH "QNX shell Program")
#set(CMAKE_AR           "${QNX_HOST}/usr/bin/${TOOLCHAIN_PREFIX}ar"      CACHE PATH "QNX ar Program")
#set(CMAKE_RANLIB       "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-ranlib"  CACHE PATH "QNX ranlib Program")
#set(CMAKE_NM           "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-nm"      CACHE PATH "QNX nm Program")
#set(CMAKE_OBJCOPY      "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-objcopy" CACHE PATH "QNX objcopy Program")
#set(CMAKE_OBJDUMP      "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-objdump" CACHE PATH "QNX objdump Program")
#set(CMAKE_LINKER       "${QNX_HOST}/usr/bin/${TOOLCHAIN_PREFIX}ld -fuse-ld=gold:.gold"      CACHE PATH "QNX Linker Program")
#set(CMAKE_STRIP        "${QNX_HOST}/usr/bin/nto${CMAKE_SYSTEM_PROCESSOR}-strip"   CACHE PATH "QNX Strip Program")

set(CMAKE_C_FLAGS_INIT " -B${QNX_TARGET} -D__QNX__ -D__QNXNTO__ -D__NO_INLINE__ -D__DEPRECATED -D__unix__ -D__unix -D__ELF__ -D__LITTLEENDIAN__ -D__AARCH64EL__=1 -D__aarch64__=1 ")
set(CMAKE_CXX_FLAGS_INIT " -B${QNX_TARGET} -D__QNX__ -D__QNXNTO__ -D__NO_INLINE__ -D__DEPRECATED -D__unix__ -D__unix -D__ELF__ -D__LITTLEENDIAN__ -D__AARCH64EL__=1 -D__aarch64__=1 ")
# Without that flag CMake is not able to pass test compilation check
if (${CMAKE_VERSION} VERSION_EQUAL "3.6.0" OR ${CMAKE_VERSION} VERSION_GREATER "3.6")
    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
else()
    set(CMAKE_EXE_LINKER_FLAGS_INIT "-nostdlib")
endif()

set(CMAKE_OBJCOPY llvm-objcopy CACHE INTERNAL "objcopy tool")
set(CMAKE_SIZE_UTIL llvm-size CACHE INTERNAL "size tool")

# Default C compiler flags
set(CMAKE_C_FLAGS_DEBUG_INIT "-g3 -Og -Wall -pedantic -DDEBUG")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG_INIT}" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_RELEASE_INIT "-O3 -Wall")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE_INIT}" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_MINSIZEREL_INIT "-Oz -Wall")
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL_INIT}" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT  "-O2 -g -Wall")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO_INIT}" CACHE STRING "" FORCE)

# Default C++ compiler flags
set(TOOLCHAIN_CXX_INCLUDE_DIRS_FLAG "")
string(APPEND TOOLCHAIN_CXX_INCLUDE_DIRS_FLAG " -cxx-isystem ${QNX_TARGET}/usr/include/c++/v1")
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-g3 -Og -Wall -pedantic -DDEBUG ${TOOLCHAIN_CXX_INCLUDE_DIRS_FLAG}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG_INIT}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -Wall ${TOOLCHAIN_CXX_INCLUDE_DIRS_FLAG}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE_INIT}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_MINSIZEREL_INIT "-Oz -Wall ${TOOLCHAIN_CXX_INCLUDE_DIRS_FLAG}")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL_INIT}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT  "-O2 -g -Wall ${TOOLCHAIN_CXX_INCLUDE_DIRS_FLAG}")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT}" CACHE STRING "" FORCE)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_SYSROOT ${QNX_TARGET})
set(CMAKE_FIND_ROOT_PATH ${QNX_TARGET})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
